//
//  RechargeVerifyViewController.m
//  SP2P
//
//  Created by 吴建东 on 2016/10/25.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "RechargeVerifyViewController.h"

@interface RechargeVerifyViewController ()<HTTPClientDelegate>
@property (weak, nonatomic) IBOutlet UILabel     *mobileLabel;
@property (weak, nonatomic) IBOutlet UILabel     *verifyLabel;
@property (weak, nonatomic) IBOutlet UITextField *verifyNumTextfield;
@property (weak, nonatomic) IBOutlet ClickButton *nextButton;

@property (nonatomic, strong) NetWorkClient *requestClient;
@property (nonatomic, assign) BOOL          isLoading;
@property (nonatomic, assign) NSInteger     requestType;
@end

@implementation RechargeVerifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initNavBarTitle:@"快捷充值" andLeftItemName:@"返回" andRightItemName:@""];
    self.view.backgroundColor = [UIColor whiteColor];
   
    [self initData];
    [self initView];
    
}

#pragma mark - Action
- (IBAction)nextButtonClick:(id)sender {
    if (_verifyNumTextfield.text.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    [_verifyNumTextfield resignFirstResponder];
    [SVProgressHUD showWithStatus:@"正在处理中"];
    [self submitRequest];
}

#pragma mark - Data
- (void)initData {
    
}

- (void)submitRequest {
    if (_isLoading) {
        [SVProgressHUD showImage:nil status:@"正在请求,请稍后再试"];
        return;
    }
    _requestType = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"523" forKey:@"OPT"];
    [parameters setObject:@"" forKey:@"body"];
    [parameters setObject:_tradeNoStr forKey:@"outer_trade_no"];
    [parameters setObject:_uuidStr forKey:@"uuid"];
    [parameters setObject:_verifyNumTextfield.text forKey:@"code"];
    
    if (_requestClient == nil) {
        _requestClient = [[NetWorkClient alloc] init];
        _requestClient.delegate = self;
    }
    [_requestClient requestGet:@"app/services" withParameters:parameters];
}

//HTTPClientDelegate
- (void)startRequest {
    _isLoading = YES;
}

-(void) httpResponseSuccess:(NetWorkClient *)client dataTask:(NSURLSessionDataTask *)task didSuccessWithObject:(id)obj
{
    _isLoading = NO;
    NSDictionary *dics=(NSDictionary *)obj;
    DLOG(@"dic=%@",dics);
    NSString *errorStr=[NSString stringWithFormat:@"%@",[dics objectForKey:@"error"]];
    if ([errorStr isEqualToString:@"-1"]) {
        
        if (_requestType == 1) {
            AppDelegateInstance.userInfo.hasRecharge = YES;
            
            [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@",[dics objectForKey:@"msg"]]];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 4] animated:YES];

            });
        }
        
    }else if ([[NSString stringWithFormat:@"%@",[dics objectForKey:@"error"]] isEqualToString:@"-2"]) {
        
        [ReLogin outTheTimeRelogin:self];
    }else{
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",[dics objectForKey:@"msg"]]];
    }
}

-(void) httpResponseFailure:(NetWorkClient *)client dataTask:(NSURLSessionDataTask *)task didFailWithError:(NSError *)error {
    _isLoading = NO;
    [SVProgressHUD showErrorWithStatus:@"连接服务器失败，请稍后再试"];

}

-(void) networkError {
    _isLoading = NO;
    [SVProgressHUD showErrorWithStatus:@"网络不给力，请检查您的网络设置"];
    
}


#pragma mark - View
- (void)initView {
    _nextButton.clipsToBounds = YES;
    _nextButton.layer.cornerRadius = kCornerRadius;
    
    _mobileLabel.font = KFontSize(14.0);
    _verifyLabel.font = KFontSize(15.0);
    _verifyNumTextfield.font = KFontSize(14.0);
    _nextButton.titleLabel.font = KFontSize(16.0);
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
